﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using System.Drawing;
using BenzInfo.Hockey.Grenadiers.Utils;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class ContestController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        private const string BASE_PATH = "~/Content/images/posts";

        private const string ContestKey = "Contest";

        //
        // GET: /Contest/

        public ViewResult Index()
        {
            List<Post> posts = db.Posts.Where(p => p.Key == ContestKey).ToList();

            if (posts.Count == 0)
            {
                Post post = new Post()
                {
                    Key = ContestKey,
                    TitleEN = "Contest",
                    TitleFR = "Concours",
                    ContentEN = "Contest",
                    ContentFR = "Concours",
                    UserCreatedBy = ViewBag.CurrentUser,
                    DateCreated = DateTime.Now,
                    UserModifiedBy = ViewBag.CurrentUser,
                    DateModified = DateTime.Now
                };

                db.Posts.Attach(post);
                db.Posts.Add(post);
                db.SaveChanges();
                posts.Add(post);
            }

            return View(posts);
        }
        //
        // GET: /Shop/Edit/5
        public ActionResult Add()
        {
            return PartialView();
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Add(Post news, HttpPostedFileBase inputFile)
        {
            if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{0:000}.{1}", DateTime.Now.Ticks, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    news.Picture = filename;
                }
            }

            news.UserCreatedBy = ViewBag.CurrentUser;
            news.DateCreated = DateTime.Now;
            news.UserModifiedBy = ViewBag.CurrentUser;
            news.DateModified = DateTime.Now;
            news.Key = ContestKey;

            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");
            ModelState.Remove("Key");

            if (ModelState.IsValid)
            {
                db.Posts.Attach(news);
                db.Posts.Add(news);
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }         
        //
        // GET: /Contest/Edit/5

        public ActionResult Edit(int id)
        {
            Post post = db.Posts.Find(id);

            return PartialView("Edit", post);
        }

        //
        // POST: /Contest/Edit/5

        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(Post post, HttpPostedFileBase inputFile, string nopicture)
        {
            if (nopicture == "on")
            {
                post.Picture = null;
            }
            else if (inputFile != null)
            {
                using (Image img = Image.FromStream(inputFile.InputStream))
                {
                    string ext = inputFile.FileName.Split('.')[inputFile.FileName.Split('.').Length - 1];
                    string filename = string.Format("{1}_{0:000}.{2}", DateTime.Now.Ticks, post.Id, ext);
                    string filepath = string.Format("{0}/{1}", Server.MapPath(BASE_PATH), filename);

                    ImageHelper.Save(img, filepath, ext);

                    post.Picture = filename;
                }
            }

            Post postdb = db.Posts.Find(post.Id);

            post.UserModifiedBy = ViewBag.CurrentUser;
            post.DateModified = DateTime.Now;

            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");

            TryUpdateModel(postdb);

            if (ModelState.IsValid)
            {
                postdb.Picture = post.Picture; // Quickfix...
                db.Posts.Attach(postdb);
                db.Entry(postdb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(post);
        }
        public ActionResult Delete(int id)
        {
            Post post = db.Posts.Find(id);
            db.Posts.Remove(post);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}